package com.xuduo.amd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuduo.amd.dao.EamApplicationDao;
import com.xuduo.amd.dao.EamBusinessDao;
import com.xuduo.amd.dto.ApplicationInfoResp;
import com.xuduo.amd.dto.BusinessRequest;
import com.xuduo.amd.dto.MapRequest;
import com.xuduo.amd.dto.SelectRequest;
import com.xuduo.amd.entity.EamBusinessEntity;
import com.xuduo.amd.pojo.BusinessInfoResp;
import com.xuduo.amd.pojo.EamBusinessBo;
import com.xuduo.amd.pojo.SelectEntity;
import com.xuduo.amd.service.EamBusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.security.Key;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EamBusinessServiceImpl implements EamBusinessService {
    private static String tkhealth = "泰康健康";

    @Autowired(required = false)
    private EamBusinessDao eamBusinessDao;

    @Autowired
    private EamApplicationDao eamApplicationDao;


    @Override
    public Map<String, List<String>> mapSelect() {
        Map<String, List<String>> result = new HashMap<>();
        List<String> svl = eamBusinessDao.selectByOne();
        List<String> business = eamBusinessDao.selectByTwo();
        List<String> status = eamBusinessDao.findByStatus();
        List<String> group = eamBusinessDao.findByGroup();
        result.put("系统归属", business);
        result.put("系统分层", svl);
        result.put("系统状态", status);
        result.put("系统群组", group);

        return result;
    }

    @Override
    public Map<String, Object> controllerInfo(MapRequest request) {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> top = eamBusinessDao.findByConntrollerTop(request.getBusinessLine());
        int x = 0;
        int s = 0;
        for (int i = 0; i < top.size(); i++) {
            s = Integer.valueOf(top.get(i).get("nums").toString());
            x = x + s;
            if (!StringUtils.isEmpty(top.get(i).get("status"))) {
                if (("生产中").equals(top.get(i).get("status").toString())) {
                    result.put(top.get(i).get("status").toString(), top.get(i).get("nums"));
                }
                if (("规划中").equals(top.get(i).get("status").toString())) {
                    result.put(top.get(i).get("status").toString(), top.get(i).get("nums"));
                }
                if (("实施中").equals(top.get(i).get("status").toString())) {
                    result.put(top.get(i).get("status").toString(), top.get(i).get("nums"));
                }

            }
        }
        result.put("基线总数", x);
        return result;
    }

    @Override
    public Map<String, List<Map<String, Object>>> findControllerMid(MapRequest request) {
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        List<Map<String, Object>> mid1 = eamBusinessDao.findControllerMid(request.getBusinessLine());
        List<Map<String, Object>> mid2 = eamBusinessDao.findControllerMidTwo(request.getBusinessLine());
        List<Map<String, Object>> mid3 = eamBusinessDao.findControllerMidThree(request.getBusinessLine());
        List<Map<String, Object>> mid4 = eamBusinessDao.findControllerMidFour();
        result.put("研发部门", mid1);
        result.put("系统分层", mid2);
        result.put("业务板块", mid3);
        result.put("集团分布", mid4);
        return result;
    }


    @Override
    public Map<String, Object> select() {
        List<Map<String, String>> result = eamBusinessDao.select();
        Set<String> key1 = new HashSet<>();
        Set<String> key2 = new HashSet<>();
        Set<String> key3 = new HashSet<>();
        Set<String> key4 = new HashSet<>();
        Set<String> key5 = new HashSet<>();
        Set<String> key6 = new HashSet<>();
        for (int i = 0; i < result.size(); i++) {

            if (!StringUtils.isEmpty(result.get(i))) {
                if (!StringUtils.isEmpty(result.get(i).get("dept_dev"))) {
                    key1.add(result.get(i).get("dept_dev"));
                }
                if (!StringUtils.isEmpty(result.get(i).get("block_strategy"))) {
                    key2.add(result.get(i).get("block_strategy"));
                }
                if (!StringUtils.isEmpty(result.get(i).get("sys_lvl"))) {
                    key3.add(result.get(i).get("sys_lvl"));
                }
                if (!StringUtils.isEmpty(result.get(i).get("sys_group"))) {
                    key4.add(result.get(i).get("sys_group"));
                }
                if (!StringUtils.isEmpty(result.get(i).get("status"))) {
                    key5.add(result.get(i).get("status"));
                }
                if (!StringUtils.isEmpty(result.get(i).get("dept_use"))) {
                    key6.add(result.get(i).get("dept_use"));
                }
            }
        }

        Map<String, Object> result2 = new HashMap<>();
        result2.put("dept_dev", key1);
        result2.put("block_strategy", key2);
        result2.put("sys_lvl", key3);
        result2.put("sys_group", key4);
        result2.put("status", key5);
        result2.put("dept_use", key6);
        return result2;
    }

    @Override
    public PageInfo<SelectEntity> findByConditions(SelectRequest request) {
        int pageNum = request.getPageMum();
        int pagSize = request.getPageSize();
        PageHelper.startPage(pageNum, pagSize);
        List<SelectEntity> result = eamBusinessDao.findByCondition(request);
        PageInfo<SelectEntity> pageInfo = new PageInfo<>(result);
        return pageInfo;
    }

    @Override
    public Map<String, Map<String, List<String>>> selectMap(MapRequest request) {
        EamBusinessEntity entity = new EamBusinessEntity();
        BeanUtils.copyProperties(request, entity);
        List<EamBusinessBo> list = eamBusinessDao.finaAll(entity);
        List<String> tempList = new ArrayList<>();

        list.stream().forEach(bo -> {
            if (StringUtils.isEmpty(bo.getSysLvl())) {
                bo.setSysLvl("其他");
            }
            if (StringUtils.isEmpty(bo.getSysGroup())) {
                bo.setSysGroup("其他");
            }
        });
        Map<String, Map<String, List<String>>> newMap = new LinkedHashMap<>();
        newMap.put("渠道应用层", null);
        newMap.put("业务应用层", null);
        newMap.put("核心服务层", null);
        newMap.put("数据整合层", null);
        newMap.put("运营管理层", null);
        newMap.put("基础服务层", null);

        Map<String, List<EamBusinessBo>> map = list.stream().collect(Collectors.groupingBy(EamBusinessBo::getSysLvl));
        map.forEach((key, value) -> {
            Map<String, List<String>> map2 = new LinkedHashMap<>();
            map2.put("集团总部", tempList);
            map2.put("泰康人寿", tempList);
            map2.put("泰康养老", tempList);
            map2.put("泰康在线", tempList);
            map2.put("泰康健投", tempList);
            map2.put("泰康资产", tempList);
            Map<String, List<EamBusinessBo>> newValue = value.stream().collect(Collectors.groupingBy(EamBusinessBo::getBusinessLine));
            newValue.forEach((key1, value1) -> {
                Map<String, List<EamBusinessBo>> map1 = value1.stream().collect(Collectors.groupingBy(EamBusinessBo::getSysGroup));
                List<String> newList = new ArrayList<>();
                map1.forEach((key2, value2) -> {
                    newList.add(key2 + "(" + value2.size() + ")");
                });
                if (!tkhealth.equals(key1)) {
                    map2.put(key1, newList);

                }
            });

            newMap.put(key, map2);
        });


        return newMap;
    }


    @Override
    public Map<String, Map<String, List<EamBusinessBo>>> selectMapByBusiness(MapRequest request) {

        EamBusinessEntity entity = new EamBusinessEntity();
        BeanUtils.copyProperties(request, entity);
        List<EamBusinessBo> list = eamBusinessDao.finaAll(entity);
        list.stream().forEach(v -> {
            if (StringUtils.isEmpty(v.getSysGroup())) {
                v.setSysGroup("其他");
            }
            if (StringUtils.isEmpty(v.getSysLvl())) {
                v.setSysLvl("其他");
            }

        });

        Map<String, Map<String, List<EamBusinessBo>>> map = new LinkedHashMap<>();
        map.put("渠道应用层", null);
        map.put("业务应用层", null);
        map.put("核心服务层", null);
        map.put("数据整合层", null);
        map.put("运营管理层", null);
        map.put("基础服务层", null);

        Map<String, List<EamBusinessBo>> map1 = list.stream().collect(Collectors.groupingBy(EamBusinessBo::getSysLvl));
        map.forEach((key, value) -> {
            map1.forEach((key1, value1) -> {
                Map<String, List<EamBusinessBo>> newValue = value1.stream().collect(Collectors.groupingBy(EamBusinessBo::getSysGroup));
                if (key.equals(key1)) {
                    map.put(key1, newValue);
                }
            });
        });

        return map;
    }


    @Override
    public BusinessInfoResp findBusinessInfoById(String id) {
        BusinessInfoResp resp = eamBusinessDao.findBusinessInfoById();
        return resp;
    }

    @Override
    public List<ApplicationInfoResp> FindApplicationInfoById(String id) {
        List<ApplicationInfoResp> list = eamApplicationDao.findApplicationIfoById(id);
        return list;
    }

    @Override
    public int insertBach(List<EamBusinessEntity> records) {

        return eamBusinessDao.insertBach(records);
    }

    @Override
    public Map<String, List<EamBusinessEntity>> selectModel(BusinessRequest request) {
        Map<String, List<EamBusinessEntity>> result = new HashMap<>();
        List<EamBusinessEntity> source = new ArrayList<>();
        List<EamBusinessEntity> target = new ArrayList<>();
        List<String> list = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        list.add("子女教育与养老规划系统");
        list.add("泰康养老监管验收生态系统");
        list.add("泰康人寿智能合规审查系统");
        list2.add("泰康保险微投小程序");
        list2.add("人寿ODS系统");
        list.add("ADD-泰生活小程序平台");

        list.add("ADD-知识推荐平台");
        list.add("ADD数据归档平台");
        list.add("ADD泰康健投拜博证照系统");
        list.add("人寿数据服务系统");
        list.forEach(i -> {
            EamBusinessEntity s = eamBusinessDao.selectByName(i);
            source.add(s);
        });
        list2.forEach(i -> {
            EamBusinessEntity x = eamBusinessDao.selectByName(i);
            target.add(x);
        });
        result.put("source", source);
        result.put("target", target);


        return result;
    }
}
